import Vuex from "vuex";
import request from '@/util/request.js'
import router from '@/router/index.js'

export const store = new Vuex.Store({
    // state专门用来保存 共享的状态值
    state: {
        userInfo:{},
        // wsUrl:"wss://botapi.ruojy.top/chat/",
        wsUrl:"ws://192.168.0.229:5701/chat/",
        wsTempUrl:"ws://192.168.0.229:5701/chatTemp/",
        // wsTempUrl:"wss://botapi.ruojy.top/chatTemp/",
    },
    // mutations: 专门书写方法,用来更新 state 中的值
    mutations: {
        async updateUser(state,payload) {
            state.userInfo=payload;
        },
        clearUser(state){
            request.get("/user/loginOut")
            router.push("/login")
            state.userInfo = {};
        }
    },
    actions: {
        async getLoginUser({commit, state}) {
            const res = await request.get("/user/getLoginUserVO")
            commit("updateUser",res.data.data);
        },
        clearLoginUser({commit}){
            commit("clearUser")
        }
    },
});

// 整合初始状态和变更函数，我们就得到了我们所需的 store
// 至此，这个 store 就可以连接到我们的应用中
export default store